Step 2 - Create keyboard navigation for the Home screen
In this step you create keyboard navigation to switch between the screens showing the recent and favorite destinations on the Home application screen. You also set a key to set focus to the button that opens the application screen which shows the sliders for adjusting the air conditioning temperature.
Set a key to focus elements in the Home screen
In this section you create keyboard navigation to set focus to the button which navigates to the AirCondition application screen and to navigate to the Recent and Favorite application screens when the user presses the ↑ (Up Arrow) key on their keyboard.
To set a key to focus elements in the Home screen:
In the Project select the Screen node and in the Node Components click Add new trigger for Key Down to add a Key Down trigger.
In the Trigger Settings Editor for that trigger click Add condition and in the Trigger Condition Editor set:
Value A
Value From to Property
Item to Screens/Screen/RootPage/Home
Property to Activation State
Operator to =
Value B
Value From to Fixed
Enable the Fixed Value checkbox.
In the Trigger Condition Editor click Save.
Kanzi sets off this trigger only when the Home node is active. When you press the ↑ (Up Arrow) key when any other Page node is active, the application does not navigate to the Favorite node.
In the Trigger Settings Editor click Add condition to add another condition and in the Trigger Condition Editor set:
Value A
Value From to Message
Argument to Key
Operator to =
Value B
Value From to Fixed
Fixed Value to 61
In the Trigger Condition Editor and the Trigger Settings Editor windows click Save.
Kanzi sets off this trigger when the user presses the ↑ (Up Arrow) key.
In the Node Components for the Key Down trigger you added at the beginning of this section add an Execute Script action, create a script, and in the Script Editor use this script:
// Get the Button 2D node ACButton.
airConditionButton = node.lookupNode('#ACButton');
recentPage = node.lookupNode('#Recent');
favoritePage = node.lookupNode('#Favorite');
// The value of the Page.State property tells whether the Page or Page Host node is active:
// - If the value is 0, the node is inactive and invisible.
// - If the value is 1, the node is active and visible.
recentActive = recentPage.getProperty('Page.State');
favoriteActive = favoritePage.getProperty('Page.State')
screen = node.lookupNode('#Screen');
// Get the node that has focus.
nodeInFocus = getFocusedNode();
// If the Screen node has focus, set focus to the ACButton node.
// By default when a Button node has focus, it receives click input from the keyboard keys Enter,
// Space, and Enter on the numeric pad.
// If the ACButton node has focus, set focus to the Recent or Favorite node
// based on which of those nodes is active.
if (nodeInFocus == screen)
{
airConditionButton.trySetFocus();
}
else if (nodeInFocus == airConditionButton && recentActive == 1)
{
recentPage.trySetFocus();
}
else if (nodeInFocus == airConditionButton && favoriteActive == 1)
{
favoritePage.trySetFocus();
}
You use this script to set focus to the ACButton node, Recent node or Favorite node when the user presses the ↑ (Up Arrow) key on their keyboard. When the ACButton node has focus, the user can use the Enter key on their keyboard to navigate to the AirCondition screen.
In the Project select the Screen node and in the Node Components copy the Execute Script action you created in the previous step.
In the Project > Home > HomeControls select the ACButton node, in the Node Components add a Key Down trigger, and paste the Execute Script action. When the ACButton node has focus and the user presses the ↑ (Up Arrow) key on their keyboard, you execute the same script on that node. This allows you to set focus from the ACButton node to the Recent or Favorite node.
In the Trigger Settings Editor for the Key Down trigger you created in the previous step add conditions:
A condition which sets off the trigger only when the Home node is active. In the Trigger Condition Editor set:
Value A
Value From to Property
Item to Screens/Screen/RootPage/Home
Property to Activation State
Operator to =
Value B
Value From to Fixed
Enable the Fixed Value checkbox.
In the Trigger Condition Editor click Save.
A condition which sets off the trigger when the user presses the ↑ (Up Arrow) key. In the Trigger Condition Editor set:
Value A
Value From to Message
Argument to Key
Operator to =
Value B
Value From to Fixed
Fixed Value to 61
In the Trigger Condition Editor and the Trigger Settings Editor windows click Save.
In the Preview when you press the ↑ (Up Arrow) key on the keyboard when the Home screen is active, you:
Set focus to the button which you use to navigate to the AirCondition application screen. The ACButton node uses a state manager which highlights the button when it has focus.
Set focus and navigate to the Recent or Favorite screen based on which of those screens is active. The Recent and Favorite nodes use state managers which highlight those pages when they have focus.
In the Preview when the ACButton is focused, press the Enter key on the keyboard to navigate to the AirCondition application screen.
Set a keyboard key to focus the navigation bar
In this section you add the functionality to move the focus from the content of the Home application screen to the navigation bar.
To use keyboard keys to set focus to the navigation bar:
In the Project select the Home > RootPage > Home > Recent node and in the Node Components add a Key Down trigger.
In the Trigger Settings Editor for the trigger you created in the previous step add conditions:
A condition which sets off the trigger only when the Home node is active. In the Trigger Condition Editor set:
Value A
Value From to Property
Item to Screens/Screen/RootPage/Home
Property to Activation State
Operator to =
Value B
Value From to Fixed
Enable the Fixed Value checkbox.
In the Trigger Condition Editor click Save.
A condition which sets off the trigger when the user presses the ↓ (Down Arrow) key on their keyboard. In the Trigger Condition Editor set:
Value A
Value From to Message
Operator to =
Value B
Value From to Fixed
Fixed Value to 62
In the Trigger Condition Editor and the Trigger Settings Editor windows click Save.
In the Node Components for the Key Down trigger you created at the beginning of this section add an Execute Script action, create a script, and in the Script Editor use this script:
// Get the Button 2D node ACButton.
airConditionButton = node.lookupNode('#ACButton');
recentPage = node.lookupNode('#Recent');
favoritePage = node.lookupNode('#Favorite');
// The value of the Page.State property tells whether the Page or Page Host node is active:
// - If the value is 0, the node is inactive and invisible.
// - If the value is 1, the node is active and visible.
recentActive = recentPage.getProperty('Page.State');
favoriteActive = favoritePage.getProperty('Page.State')
screen = node.lookupNode('#Screen');
// Get the node that has focus.
nodeInFocus = getFocusedNode();
// If the Page nodes Recent or Favorite have focus, set focus to the Button 2D node ACButton.
// If the Button 2D node ACButton has focus, set focus to the Screen node.
if (nodeInFocus == recentPage)
{
airConditionButton.trySetFocus();
}
else if (nodeInFocus == favoritePage)
{
airConditionButton.trySetFocus();
}
else if (nodeInFocus == airConditionButton)
{
screen.trySetFocus();
}
You use this script to set focus to the ACButton node or Screen node when the user presses the ↓ (Down Arrow) key on their keyboard. When the focus is on the Screen, the user can use the → (Right Arrow) and ← (Left Arrow) keyboard keys to navigate from the Home application screen to the Car, Media, and Navigation application screens. In the next section you create scripts to use keyboard arrow keys to navigate between the Recent and Favorite application screens.
In the Project select the Recent node and in the Node Components copy and paste the Key Down trigger to the Favorite node. You use the same trigger to set focus to the ACButton node when the Favorite node has focus and the user presses the ↓ (Down Arrow) key.
In the Project select the Recent node and in the Node Components from the Key Down trigger copy the Execute Script action you created in step 3.
In the Project select the ACButton node, in the Node Components click Add new trigger for Key Down and to that trigger paste the Execute Script action you copied.
In the Trigger Settings Editor for the trigger you created in the previous step add a condition which sets off the trigger when the user presses the ↓ (Down Arrow) key on their keyboard. In the Trigger Condition Editor set:
Value A
Value From to Message
Operator to =
Value B
Value From to Fixed
Fixed Value to 62
In the Trigger Condition Editor and the Trigger Settings Editor windows click Save.
In the Preview when you press the ↓ (Down Arrow) key on the keyboard when the Recent or Favorite screen has focus, you:
Set focus to the button which you use to navigate to the AirCondition application screen.
Set focus to the navigation bar. You can use the → (Right Arrow) and ← (Left Arrow) keyboard keys to navigate to other application screens on the navigation bar.
Create keyboard navigation between the Recent and Favorite screens
In this section you set the application to navigate between the Recent and Favorite screens when the user presses a key on the keyboard.
To create keyboard navigation between the Recent and Favorite screens:
In the Project > RootPage > Home select the Recent node and in the Node Components click Add new trigger for Key Down.
In the Trigger Settings Editor for that trigger click Add condition and add conditions:
A condition which sets off the trigger only when the Page Host node Home is active. In the Trigger Condition Editor set:
Value A
Value From to Property
Item to Screens/Screen/RootPage/Home
Property to Activation State
Operator to =
Value B
Value From to Fixed
Enable the Fixed Value checkbox.
In the Trigger Condition Editor click Save.
A condition which sets off the trigger when the user presses the → (Right Arrow) key. In the Trigger Condition Editor set:
Value A
Value From to Message
Operator to =
Value B
Value From to Fixed
Fixed Value to 64
In the Trigger Condition Editor and the Trigger Settings Editor windows click Save.
In the Node Components for the Key Down trigger add an Execute Script action, create a script, and in the Script Editor use this script:
// Get the Page nodes Recent and Favorite.
recentPage = node.lookupNode('#Recent');
favoritePage = node.lookupNode('#Favorite');
// The value of the Page.State property tells whether the Page or Page Host node is active:
// - If the value is 0, the node is inactive and invisible.
// - If the value is 1, the node is active and visible.
recentActive = recentPage.getProperty('Page.State');
favoriteActive = favoritePage.getProperty('Page.State')
// If the Page node Recent is active, set focus to the Page node Favorite and navigate to that page.
if (recentActive == 1)
{
favoritePage.trySetFocus();
favoritePage.navigateToThisPage();
}
You use this script to navigate from the Recent application screen to the Favorite application screen when the user presses the → (Right Arrow) key on their keyboard.
In the Project > RootPage > Home select the Favorite node and in the Node Components click Add new trigger for Key Down.
In the Trigger Settings Editor for that trigger click Add condition and add two conditions:
A condition which sets off the trigger only when the Page Host node Home is active.
A condition which sets off the trigger when the user presses the ← (Left Arrow) key.
In the Node Components for the Key Down trigger add an Execute Script action, create a script, and in the Script Editor use this script:
// Get the Page nodes Recent and Favorite.
recentPage = node.lookupNode('#Recent');
favoritePage = node.lookupNode('#Favorite');
// The value of the Page.State property tells whether the Page or Page Host node is active:
// - If the value is 0, the node is inactive and invisible.
// - If the value is 1, the node is active and visible.
recentActive = recentPage.getProperty('Page.State');
favoriteActive = favoritePage.getProperty('Page.State')
// If the Page node Favorite is active, set focus to the Page node Recent and navigate to that page.
if (favoriteActive == 1)
{
recentPage.trySetFocus();
recentPage.navigateToThisPage();
}
You use this script to navigate from the Favorite application screen to the Recent application screen when the user presses the ← (Left Arrow) key on their keyboard.
Set focus to the Recent and Favorite nodes when the user navigates to one of those pages with the pointer:
From the Recent node copy the Execute Script trigger you created in this section and paste it to Project > RootPage > Home > HomeControls > Stack Layout 2D > FavoriteButton node to the Button: Click trigger.
From the Favorite node copy the Execute Script action you created in this section and paste it to the Project > RootPage > Home > HomeControls > Stack Layout 2D > RecentButton node to the Button: Click trigger.
You set focus to the Recent or Favorite node when the user clicks the RecentButton or FavoriteButton node to navigate to one of those pages. This way the user can use the keyboard keys to continue navigating in the application.
In the Project select the Recent node and in the Node Components from the Button: Click trigger remove the Navigate to Page action. Because the script you execute on the RecentButton node navigates to the Page node Favorite, that node no longer needs the Navigate to Page action.
Repeat the previous step for the Favorite node.
In the Preview when the Recent or Favorite application screen has focus, use the → (Right Arrow) and ← (Left Arrow) keyboard keys to navigate between those screens.
Set a key to close the air conditioning settings
In this section you set the AirCondition screen to close when the user presses a key on their keyboard.
To set a key to close the air conditioning settings:
In the Project > RootPage > Home > AirCondition select the Driver node and in the Node Components add a Key Down trigger.
In the Trigger Settings for the trigger you created in the previous step click Add condition and in the Trigger Condition Editor set:
Value A
Value From to Message
Operator to =
Value B
Value From to Fixed
Fixed Value to 19
In the Trigger Condition Editor and the Trigger Settings Editor windows click Save.
You set the application to close the AirCondition window when the user presses the E key.
Add to the trigger an Execute Script action, create a script, and in the Script Editor use this script:
// Get the Screen node.
screen = node.lookupNode('#Screen');
// Set focus to the Screen node.
screen.trySetFocus();
You set the application to execute a script which sets focus to the Screen when the trigger is set off. This way the focus does not stay on the Driver or Passenger nodes when the AirCondition application screen closes. At the end of this tutorial step you set focus to the Driver node when the user navigates to the AirCondition application screen.
Add to the trigger a Navigate to Page action and set the Item to Screens/Screen/RootPage/Home/Recent. You set the application to navigate to the Recent node in the Home node.
In the Project > RootPage > Home > AirCondition > Driver select the Slider 2D node and in the Node Components add a Key Down trigger. You use this trigger to close the AirCondition window when the Slider 2D node has focus.
In the Trigger Settings for the trigger you created in the previous step click Add condition and in the Trigger Condition Editor set:
Value A
Value From to Message
Operator to =
Value B
Value From to Fixed
Fixed Value to 19
In the Trigger Condition Editor and the Trigger Settings Editor windows click Save.
You set the application to close the AirCondition window when the user presses the E key.
Add to the Key Down trigger you created in the previous step the actions you need to close the AirCondition window when the Slider 2D node has focus:
An Execute Script action and use the Script_4.js script you created in step 3. You set Kanzi to execute a script which sets focus to the Screen when the trigger is set off. This way the focus does not stay on the Slider 2D node when the AirCondition window closes.
A Navigate to Page action and set the Item to Screens/Screen/RootPage/Home/Recent. You set Kanzi to navigate to the Page node Recent.
Select the Slider 2D node and from the Node Components copy and paste the Key Down trigger you created in the previous steps to the Project > RootPage > Home > AirCondition > Passenger > Slider 2D node. You use the same trigger for the Slider 2D on the Passenger node to close the AirCondition window when that Slider 2D node has focus.
In the Project > RootPage > Home select the AirCondition node and in the Node Components add to the Page Activated trigger an Execute Script action. The Page Activated trigger is set off when a Page or Page Host node becomes active.
Create a script and in the Script Editor use this script:
// Get the Page node Driver.
var driverPage = node.lookupNode('#Driver');
// Set focus to the Page node Driver.
driverPage.trySetFocus();
You use this script to set focus to the Page node Driver when the Page Host node AirCondition is activated. When you navigate to the Driver page, you can use the same keyboard keys which you have defined in other parts of the application. In the next step of this tutorial you set the → and ← keys to navigate between the Driver and Passenger application screens.
In the Preview in the AirCondition application screen press the E key on the keyboard to close that screen.
Use these keyboard keys to navigate the application:
← (Left Arrow) and → (Right Arrow) keys
Navigate between the Home, Media, Car, and Navigation application screens.
When the Recent or Favorite application screen has focus, navigate between those application screens.
↑ (Up Arrow) and ↓ (Down Arrow) keys
Set focus to the button which opens the AirCondition application screen.
Set focus to the Recent or Favorite application screen based on which of those application screens is active.
Enter key navigates to the AirCondition application screen when the ACButton node has focus.
E key closes the AirCondition application screen.
Organize the script files in your Kanzi Studio project
In this section you rename the scripts you created in this step of the tutorial and create folders for those scripts.
To organize the script files in your Kanzi Studio project:
In the Library > Resource Files > Scripts rename:
Script.js to NavigateUp.js
Script_1.js to NavigateDown.js
Script_2.js to OnRecentRight.js
Script_3.js to OnFavoriteLeft.js
Script_4.js to SetFocusToScreen.js and drag it to the Screen folder
Script_5.js to OnAirConditionActivated.js
In the Library > Resource Files > Scripts create a folder, name it Home, and drag the NavigateUp.js, NavigateDown.js, OnRecentRight.js, and OnFavoriteLeft.js scripts to that folder. You use this folder to store those JavaScript scripts which you call on child nodes of the Home node.
Repeat the previous step to create another folder, name it AirCondition, and drag the OnAirConditionActivated.js script to that folder.